package com.xyd.service.impl;

import com.xyd.entity.News;
import com.xyd.mapper.NewsMapper;
import com.xyd.service.NewsService;
import com.xyd.util.MybatisUtil;

import org.apache.ibatis.session.SqlSession;


import java.util.List;

/**
 * 类描述：
 *
 * @author 张吉哲
 * @date 2021/1/11 16:30
 */
public class NewsServiceImpl implements NewsService {
    SqlSession sqlSession=null;

    /**
     * 根据ID查询全部的新闻信息
     * @param enId
     * @return
     */
    @Override
    public News getAllNews(Long enId) {
        sqlSession=MybatisUtil.getSqlSession();
        News news=sqlSession.getMapper(NewsMapper.class).getAllNews(enId);

        sqlSession.commit();
        MybatisUtil.closeSqlSession(sqlSession);
        return news;
    }

    /**
     * 得到所有新闻信息
     * @return
     */
    @Override
    public Integer getNewsCount() {
        sqlSession=MybatisUtil.getSqlSession();
        int count=sqlSession.getMapper(NewsMapper.class).getNewsCount();
        MybatisUtil.closeSqlSession(sqlSession);
        return count;
    }

    /**
     * @param pageIndex 开始的页数
     * @param pageSize   每页显示多少条
     * @return
     */
    @Override
    public List<News> getNewsByPage(Integer pageIndex, Integer pageSize) {
        sqlSession=MybatisUtil.getSqlSession();
        List<News>newsList=sqlSession.getMapper(NewsMapper.class).getNewsByPage(pageIndex,pageSize);
        MybatisUtil.closeSqlSession(sqlSession);
        return newsList;
    }

    /**
     * 添加新闻信息
     * @param news
     * @return 是BoOlen类型判断是否添加成功
     */

    @Override
    public Boolean addNews(News news) {
        sqlSession=MybatisUtil.getSqlSession();

        Boolean flag=sqlSession.getMapper(NewsMapper.class).addNews(news);
        sqlSession.commit();
        MybatisUtil.closeSqlSession(sqlSession);
        return flag;
    }


    /**
     * 修改新闻信息
     */
    @Override
    public Boolean updateNews(News news) {
        sqlSession=MybatisUtil.getSqlSession();

        Boolean flag=sqlSession.getMapper(NewsMapper.class).updateNews(news);
        sqlSession.commit();
        MybatisUtil.closeSqlSession(sqlSession);
        return flag;
    }

    /**
     * 热点新闻
     */
    @Override
    public List<News> visitNews(Integer pageIndex,Integer pageSize) {
        sqlSession=MybatisUtil.getSqlSession();
        List<News> newsList=sqlSession.getMapper(NewsMapper.class).visitNews(pageIndex,pageSize);
        MybatisUtil.closeSqlSession(sqlSession);
        return newsList;
    }

    /**
     * 根据ID查看热点新闻
     */
    @Override
    public News visitSearchNews(Long enId) {
        sqlSession=MybatisUtil.getSqlSession();
        News newsList=sqlSession.getMapper(NewsMapper.class).visitSearchNews(enId);
        MybatisUtil.closeSqlSession(sqlSession);
        return newsList;
    }

    /**
     * 通过ID删除新闻列表
     * @param enId
     * @return
     */
    @Override
    public Boolean delNews(Long enId) {
        sqlSession=MybatisUtil.getSqlSession();

        Boolean flag=sqlSession.getMapper(NewsMapper.class).delNews(enId);

        sqlSession.commit();
        MybatisUtil.closeSqlSession(sqlSession);

        return flag;
    }


}
